<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading" class="card-top-wrap">
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" />
        </el-tabs>

        <div class="card-wrap">
          <departTree :data="company" @shouDialogEvent="showDialogFunc" />
          <el-divider style="margin-top: 50px" />
          <!-- 树形结构 -->
          <el-tree
            :data="depart"
            :props="defaultProps"
            :default-expand-all="true"
          >
            <departTree
              slot-scope="{ data }"
              :type="3"
              :data="data"
              @updatePage="getDeaprts"
              @shouDialogEvent="showDialogFunc"
            />
          </el-tree>
        </div>
      </el-card>
      <Mydialog
        ref="mydialog"
        :show-dialog.sync="showDialog"
        :data-click="dataClick"
        @update="getDeaprts"
      />
    </div>
  </div>
</template>

<script>
import departTree from './components/tree.vue'
import Mydialog from './components/dialog.vue'
import { getDeaprts } from '@/api/department'
import { transformTree } from '@/utils/index'

export default {
  components: {
    departTree,
    Mydialog
  },
  data() {
    return {
      type: 'add',
      dataClick: {},
      showDialog: false,
      loading: false,
      activeName: 'first',
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      depart: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDeaprts()
  },
  methods: {
    async getDeaprts() {
      this.loading = true
      const { depts } = await getDeaprts()
      this.depart = transformTree(depts, '')
      this.loading = false
    },
    // 弹出层显示
    showDialogFunc(data, type = 'add') {
      this.type = type
      if (type === 'see') {
        this.$refs.mydialog.seeDepart(data.id)
      }
      this.dataClick = data
      this.showDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .card-wrap {
    padding: 30px 130px;
    font-size: 14px;
  }
}
</style>
